<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/02/01 09:31:59 $)
// $RCSfile: report_responsetime.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->assign("calendar", true);

$interface->staffHeader($_SWIFT["language"]["tickets"]." &gt; ".$_SWIFT["language"]["reports"], 2);

renderTicketTree();
$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; '.'<a href="index.php?_m=tickets&_a=reports&type=worksummary" title="'.$_SWIFT["language"]["reports"].'">'.$_SWIFT["language"]["reports"].'</a> &raquo; '.'<a href="index.php?_m=tickets&_a=reports&type=response" title="'.$_SWIFT["language"]["represponse"].'">'.$_SWIFT["language"]["represponse"].'</a>', "", 2);

printInfoBox($infomessage);
printErrorBox($errormessage);

?>
<form name="swiftform" action="index.php" method="POST"><fieldset class="swiftfieldset">
<legend><?=$_SWIFT["language"]["filter"]?></legend>
<table width="100%"  border="0" cellspacing="1" cellpadding="2" class="tborder">
<tr>
<?
if (empty($_POST["datefrom"]) && empty($_POST["dateto"]))
{
	$_prevday = DATENOW-86400;
	$_gdatefrom = generateCalendarTimeline($_prevday);
	$_gdateto =generateCalendarTimeline(DATENOW);
} else {
	$_gdatefrom = $_POST["datefrom"];
	$_gdateto = $_POST["dateto"];
}

?></select></td>
<td width="100" class="row1" nowrap><?=$_SWIFT["language"]["fildate"]?></td>
<td class="row2"><input type="text" name="datefrom" id="datefrom" size="12" readonly="1" value="<?=iif(!empty($_gdatefrom), $_gdatefrom)?>" class="swifttext" onclick="return showCalendar('datefrom', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" />&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" id="datefromtrigger" style="cursor: pointer;" align="absmiddle" onclick="return showCalendar('datefrom', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" /> <?=$_SWIFT["language"]["filto"]?> <input type="text" name="dateto" id="dateto" size="12" readonly="1" value="<?=iif(!empty($_gdateto), $_gdateto)?>" class="swifttext" onclick="return showCalendar('dateto', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" />&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" id="datetotrigger" style="cursor: pointer;" align="absmiddle" onclick="return showCalendar('dateto', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" /></td>
<td width="100" class="row2" align="right" nowrap><input type="submit" class="yellowbutton" name="filter" value="<?=$_SWIFT["language"]["filter"]?>"></td>
</tr>
</table>
</fieldset><BR />
<input type="hidden" name="_m" value="tickets"><input type="hidden" name="_a" value="reports"><input type="hidden" name="type" value="response">
</form>
<?
$_datefrom = getCalendarDateline($_gdatefrom);
$_dateto = getCalendarDateline($_gdateto);
$_dateto = mktime(23,59,59,date("m", $_dateto), date("d", $_dateto), date("Y", $_dateto));

// Get List of Tickets between the given time
$_tickeidlist = $_tickets = array();
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."tickets` WHERE `dateline` > '". intval($_datefrom) ."' AND `dateline` < '". intval($_dateto) ."';");
while ($dbCore->nextRecord())
{
	$_ticketidlist[] = $dbCore->Record["ticketid"];
	$_tickets[$dbCore->Record["ticketid"]] = $dbCore->Record;
}

// Build Posts
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketposts` WHERE `ticketid` IN (". buildIN($_ticketidlist) .");");
while ($dbCore->nextRecord())
{
	$_tickets[$dbCore->Record["ticketid"]]["posts"][$dbCore->Record["ticketpostid"]] = $dbCore->Record;
}


echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder"><tr class="tabletitlerow"><td align="left" width="120">'. $_SWIFT["language"]["f_ticketid"] .'</td><td align="left" width="">'. $_SWIFT["language"]["f_subject"] .'</td><td align="center" width="150">'. $_SWIFT["language"]["date"] .'</td><td align="center" width="110">'. $_SWIFT["language"]["f_ticketstatus"] .'</td><td align="center" width="110" nowrap>'. $_SWIFT["language"]["f_avgresponse"] .'</td><td align="center" width="110" nowrap>'. $_SWIFT["language"]["f_resolution"] .'</td><td align="center" width="110" nowrap>'. $_SWIFT["language"]["f_timeworked"] .'</td><td align="center" width="120">'. $_SWIFT["language"]["f_staffworked"] .'</td></tr>';
if (_is_array($_tickets))
{
	foreach ($_tickets as $key=>$val)
	{
		// Calculate Resolution Time
		if (empty($val["laststaffreplytime"]))
		{
			$_resolution = $_SWIFT["language"]["notreplied"];
		} else {
			$_resolution = strColorDate(($val["laststaffreplytime"]-$val["dateline"]), false);
		}

		// Calculate Avg Response Time
		$_avgresponse = 0;
		$_lastposttime = $val["dateline"];
		$_responsecount = 0;
		$_stafflist = array();
		$_stafflisttxt = "";
		foreach ($val["posts"] as $postkey=>$postval)
		{
//			echo "Parsing Post: ".$postval["ticketpostid"]."<BR />";
			if ($postval["creator"] == POST_CLIENT)
			{
//				echo "Setting Last Post..";
				$_lastposttime = $val["dateline"];
			} else if ($postval["creator"] == POST_STAFF && !empty($_lastposttime)) {
				$_avgresponse += ($postval["dateline"]-$_lastposttime);
				$_responsecount++;
				$_lastposttime = 0;
//				echo "1AVG RESPONSE: $_avgresponse<BR />1Response Count: $_responsecount";

				if (!in_array($postval["staffid"], $_stafflist) && !empty($postval["staffid"]))
				{
					$_stafflist[] = $postval["staffid"];
//					echo "StaffID: ".$postval["staffid"]."<BR />";
					$_stafflisttxt .= $_SWIFT["staffcache"][$postval["staffid"]]["fullname"]."<BR />";
				}
			}
		}

//		echo "AVG RESPONSE: $_avgresponse<BR />Response Count: $_responsecount";

		if (!empty($_avgresponse))
		{
			$_avgresponse = round($_avgresponse/$_responsecount);
		}
		$_avgresponsetxt = strColorDate($_avgresponse, false);

		echo '<tr class="'. getRowBG() .'"><td align="left"><a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($val["ticketid"]) .'" target="_blank">'. iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"]) .'</a></td><td align="left"><a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($val["ticketid"]) .'" target="_blank">'. $dbCore->escape($val["subject"]) .'</a></td><td align="center">'. edate($_SWIFT["settings"]["dt_datetimeformat"], $val["dateline"]) .'</td><td align="center">'. '<font color=\''. $_SWIFT["statuscache"][$val["ticketstatusid"]]["statuscolor"] .'\'>'.htmlspecialchars($_SWIFT["statuscache"][$val["ticketstatusid"]]["title"]).'</font>' .'</td><td align="center">'. $_avgresponsetxt .'</td><td align="center">'. $_resolution .'</td><td align="center">'. strColorDate($val["timeworked"]) .'</td><td align="center">'. $_stafflisttxt .'</td></tr>';
	}
}
echo '</table><BR />';

$template->assign("backurl", "index.php?_m=tickets&_a=manage");

$interface->staffFooter();
?>